/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/                                                                            _/
_/                      _/  _/                  _/                            _/
_/       _/_/_/    _/_/_/  _/_/_/      _/_/_/  _/_/_/      _/_/_/  _/  _/_/   _/
_/    _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/_/        _/
_/   _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/    _/  _/           _/
_/    _/_/_/    _/_/_/  _/_/_/      _/_/_/  _/_/_/      _/_/_/  _/            _/
_/       _/                                                                   _/
_/   _/_/                              Free software copyrighted since 2007   _/
_/                                              <gdbabar@gmail.com>           _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#ifdef ENABLE_NLS
#  include <glib/gi18n-lib.h>
#else
#  define _(String) (String)
#  define N_(String) (String)
#endif

#include <stdlib.h>
#include <sys/stat.h>

#include <glib/gstdio.h>
#include <gtk/gtk.h>

#include "common.h"
#include "gdbabar-config.h"

#define	MIN_WINDOW_HEIGHT 480
#define DISPLAY_PANE_NBR 4
#define PANE_ICON_SIZE 48
#define PANE_LABEL_WIDTH 114

typedef enum _PaneId
{
  DOCK_PANE_ID,
  ICON_PANE_ID,
  FONT_PANE_ID,
  ABOUT_PANE_ID
}
PaneId;

typedef struct _PaneData
  {
    gchar *name, *desc, *icon_path;
    GdkPixbuf *pixbuf, *saturate_pixbuf;
    GtkWidget *image, *label, *event_box;
    PaneId pane_id;
  }
PaneData;

typedef struct _DescriptionData
  {
    GtkWidget *h_box, *image, *label;
  }
DescriptionData;

/* pane menu prototypes */
static GtkWidget* gdbc_menu_area_new (void);
static GtkWidget* gdbc_pane_button_new (PaneData *pane_data);
static gboolean gdbc_pane_cb (GtkWidget *widget, GdkEventButton *event, PaneData *pane_data);
static void gdbc_pane_button_set_active (PaneData *pane_data, gboolean active);
static void gdbc_pane_set_desc (DescriptionData *desc_area, GdkPixbuf *pixbuf, gchar *name, gchar *desc);

/* Child window prototype */
static GtkWidget* gdbc_child_window (void);

/* bottom button prototypes */
static GtkWidget* gdbc_action_button_area_new (void);
static void import_config_file_cb (GtkButton *button, gpointer user_data);
static void export_config_file_cb (GtkButton *button, gpointer user_data);

static void create_default_conf_dir (void);

static PaneData pane_data[] =
  {
    {
      N_("Dock Settings"),
      N_("General dock settings"),
      PANE_ICONS_DIR"/bar-pane.svg",
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      DOCK_PANE_ID
    },
    {
      N_("Icons Properties"),
      N_("Add, Edit, Delete or Move dock's icons"),
      PANE_ICONS_DIR"/icons-pane.svg",
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      ICON_PANE_ID},
    {
      N_("Font Preferences"),
      N_("Font colors, size, stye and effect"),
      PANE_ICONS_DIR"/font-pane.svg",
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      FONT_PANE_ID},
    {
      N_("About"),
      N_("About authors and contributors, and how to help us"),
      PANE_ICONS_DIR"/about-pane.svg",
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      ABOUT_PANE_ID
    }
  };

static DescriptionData desc_area;
static PaneData *last_active_pane = NULL;
static GtkWidget *child_window_container, *last_child_window = NULL;

int
main (int argc, char *argv[])
{

  GtkWidget *window, *v_box;
  gchar *title;
  gint init_pane_id = DOCK_PANE_ID, i;

#ifdef ENABLE_NLS

  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);

#endif

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_icon_from_file (GDB_DEFAULT_ICON, NULL);
  gtk_window_set_default_size (GTK_WINDOW (window), -1, MIN_WINDOW_HEIGHT);

  title = g_strdup_printf (_("Gdbabar Control Panel %s") , VERSION);
  gtk_window_set_title (GTK_WINDOW (window), title);
  g_free (title);

  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_window_signal), NULL);

  if (is_first_run ())
    {
      common_infobox (GTK_WINDOW (window), _("It's the first time you run Gdbabar"));
      create_default_conf_dir ();
    }

  v_box = gtk_vbox_new (FALSE, 0);

  gtk_box_pack_start(GTK_BOX (v_box), gdbc_menu_area_new (), FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX (v_box), gdbc_child_window (), TRUE, TRUE, 0);
  gtk_box_pack_end(GTK_BOX (v_box), gdbc_action_button_area_new (), FALSE, FALSE, 8);

  for (i = 0; i < argc; i++)
    {
      if (g_ascii_strcasecmp (argv[i], "--icon-pane") == 0)
        {
          init_pane_id = ICON_PANE_ID;
        }
      else if (g_ascii_strcasecmp (argv[i], "--font-pane") == 0)
        {
          init_pane_id = FONT_PANE_ID;
        }
      else if (g_ascii_strcasecmp (argv[i], "--about-pane") == 0)
        {
          init_pane_id = ABOUT_PANE_ID;
        }
      else if (g_ascii_strcasecmp (argv[i], "--help") == 0)
        {
          g_printf (_("print command line help ;)\n"));
          exit (EXIT_SUCCESS);
        }
      else
        {
          if (i)
            {
              g_printf (_("\"%s %s\" is not recognized\n"\
                          "Use \"--help\" option to display valid command line arguments\n"),
                        argv[0],
                        argv[i]);

              exit (EXIT_FAILURE);
            }
        }
    }

  /* Set first active pane (dock settings by default) */
  gdbc_pane_cb (NULL, NULL, &pane_data[init_pane_id]);

  gtk_container_add (GTK_CONTAINER (window), v_box);

  gtk_widget_show_all (window);
  gtk_main ();

  return EXIT_SUCCESS;

}

static GtkWidget*
gdbc_menu_area_new (void)
{

  GtkWidget *v_box, *h_box, *alignment, *event_box;
  gint i;

  v_box = gtk_vbox_new (FALSE, 0);

  /* Menu bar */
  h_box = gtk_hbox_new (TRUE, 0);

  for (i = 0; i < DISPLAY_PANE_NBR; i++)
    {
      event_box = gdbc_pane_button_new (&pane_data[i]);
      g_signal_connect (event_box, "button-press-event", G_CALLBACK(gdbc_pane_cb), &pane_data[i]);
      gtk_box_pack_start (GTK_BOX (h_box), event_box, FALSE, FALSE, 0);
    }

  alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
  gtk_container_add (GTK_CONTAINER (alignment), h_box);

  gtk_box_pack_start (GTK_BOX (v_box), alignment, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (v_box), gtk_hseparator_new (), FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (v_box), gtk_hseparator_new (), FALSE, FALSE, 0);

  /* Description area */
  desc_area.h_box = gtk_hbox_new (FALSE, 0);
  desc_area.image = gtk_image_new ();
  desc_area.label = gtk_label_new (NULL);

  alignment = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
  gtk_container_add (GTK_CONTAINER (alignment), desc_area.label);

  gtk_box_pack_start(GTK_BOX (desc_area.h_box), desc_area.image, FALSE, FALSE, 8);
  gtk_box_pack_start(GTK_BOX (desc_area.h_box), alignment, TRUE, TRUE, 8);

  gtk_box_pack_start (GTK_BOX (v_box), desc_area.h_box, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (v_box), gtk_alignment_new (0.0, 0.0, 0.0, 0.0), FALSE, FALSE, 4);

  return v_box;

}

static GtkWidget*
gdbc_pane_button_new (PaneData *pane_data)
{

  GtkWidget *v_box;
  gchar *str;

  pane_data->pixbuf = gdk_pixbuf_new_from_file_at_size (pane_data->icon_path, PANE_ICON_SIZE, PANE_ICON_SIZE, NULL);

  pane_data->saturate_pixbuf = gdk_pixbuf_copy (pane_data->pixbuf);
  gdk_pixbuf_saturate_and_pixelate (pane_data->pixbuf, pane_data->saturate_pixbuf, 6.0, TRUE);

  pane_data->image = gtk_image_new ();

  gtk_image_set_from_pixbuf (GTK_IMAGE (pane_data->image), pane_data->pixbuf);

  pane_data->label = gtk_label_new (NULL);

  str = g_strdup_printf("<small>%s</small>", _(pane_data->name));
  gtk_label_set_markup (GTK_LABEL (pane_data->label), str);
  g_free (str);

  gtk_label_set_line_wrap (GTK_LABEL (pane_data->label), TRUE);
  gtk_label_set_justify (GTK_LABEL (pane_data->label), GTK_JUSTIFY_CENTER);
  gtk_widget_set_size_request (pane_data->label, PANE_LABEL_WIDTH, -1);

  pane_data->event_box = gtk_event_box_new();
  v_box = gtk_vbox_new (FALSE, 0);

  gtk_box_pack_start(GTK_BOX (v_box), pane_data->image, FALSE, FALSE, 0);
  gtk_box_pack_end(GTK_BOX (v_box), pane_data->label, FALSE, FALSE, 0);

  gtk_container_add(GTK_CONTAINER (pane_data->event_box), v_box);

  return pane_data->event_box;

}

static gboolean
gdbc_pane_cb (GtkWidget *widget, GdkEventButton *event, PaneData *pane_data)
{

  GtkWidget *child_window = NULL;

  if (last_active_pane != pane_data)
    {
      gdbc_pane_button_set_active (pane_data, TRUE);
      gdbc_pane_set_desc (&desc_area, pane_data->pixbuf, _(pane_data->name), _(pane_data->desc));

      if (last_active_pane)
        {
          gdbc_pane_button_set_active (last_active_pane, FALSE);
        }

      if (last_child_window)
        {
          gtk_widget_destroy (last_child_window);
        }

      switch (pane_data->pane_id)
        {
        case DOCK_PANE_ID:
          child_window = gdbc_dock_content_new ();
          break;
        case ICON_PANE_ID:
          child_window = gdbc_icon_content_new ();
          break;
        case FONT_PANE_ID:
          child_window = gdbc_font_content_new ();
          break;
        case ABOUT_PANE_ID:
          child_window = gdbc_about_content_new ();
          break;
        }

      if (child_window)
        {
          gtk_container_add (GTK_CONTAINER (child_window_container), child_window);
          gtk_widget_show_all (child_window_container);
        }

      last_child_window = child_window;

    }

  last_active_pane = pane_data;

  return TRUE;

}

static void
gdbc_pane_button_set_active (PaneData *pane_data, gboolean active)
{

  gchar *str;

  if (active)
    {
      str = g_strdup_printf ("<small><i><b>%s</b></i></small>", _(pane_data->name));
      gtk_image_set_from_pixbuf (GTK_IMAGE (pane_data->image), pane_data->saturate_pixbuf);
    }
  else
    {
      str = g_strdup_printf ("<small>%s</small>", _(pane_data->name));
      gtk_image_set_from_pixbuf (GTK_IMAGE (pane_data->image), pane_data->pixbuf);
    }

  gtk_label_set_markup (GTK_LABEL (pane_data->label), str);
  g_free(str);

  gtk_widget_show_all (pane_data->event_box);

}

static void
gdbc_pane_set_desc (DescriptionData *desc_area, GdkPixbuf *pixbuf, gchar *name, gchar *desc)
{

  gchar *str;

  gtk_image_set_from_pixbuf (GTK_IMAGE (desc_area->image), pixbuf);

  str = g_strdup_printf ("<b><big>%s</big></b>\n<i>%s</i>", name, desc);
  gtk_label_set_markup (GTK_LABEL (desc_area->label), str);
  g_free(str);

  gtk_widget_show_all (desc_area->h_box);

}

static GtkWidget*
gdbc_child_window (void)
{

  GtkWidget *alignment, *scrolled_window, *h_box;

  alignment = gtk_alignment_new (0, 0, 1, 0);
  gtk_container_set_border_width (GTK_CONTAINER (alignment), 6);
  child_window_container = alignment;

  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), alignment);

  h_box = gtk_hbox_new (FALSE, 0);

  gtk_box_pack_start (GTK_BOX (h_box), scrolled_window, TRUE, TRUE, 8);

  return h_box;

}

static GtkWidget*
gdbc_action_button_area_new (void)
{

  GtkWidget *h_box, *import_button, *export_button, *quit_button;

  h_box = gtk_hbox_new (FALSE, 0);

  import_button = gtk_button_new_with_mnemonic (_("_Import"));
  gtk_button_set_image (GTK_BUTTON (import_button), gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON));
  g_signal_connect (G_OBJECT (import_button), "clicked", G_CALLBACK (import_config_file_cb), NULL);
  gtk_box_pack_start (GTK_BOX (h_box), import_button, FALSE, FALSE, 8);

  export_button = gtk_button_new_with_mnemonic (_("_Export"));
  gtk_button_set_image (GTK_BUTTON (export_button), gtk_image_new_from_stock (GTK_STOCK_SAVE, GTK_ICON_SIZE_BUTTON));
  g_signal_connect (G_OBJECT (export_button), "clicked", G_CALLBACK (export_config_file_cb), NULL);
  gtk_box_pack_start (GTK_BOX (h_box), export_button, FALSE, FALSE, 0);

  quit_button = gtk_button_new_from_stock (GTK_STOCK_QUIT);
  g_signal_connect (G_OBJECT (quit_button), "clicked", G_CALLBACK (destroy_window_signal), NULL);
  gtk_box_pack_end (GTK_BOX (h_box), quit_button, FALSE, FALSE, 8);

  return h_box;

}

static void
import_config_file_cb (GtkButton *button, gpointer user_data)
{

  GtkWidget *chooser, *parent;
  gchar *filename = NULL;

  parent = gtk_widget_get_toplevel (GTK_WIDGET (button));

  chooser = gtk_file_chooser_dialog_new ( _("Import configuration file ..."),
                                          GTK_WINDOW (parent),
                                          GTK_FILE_CHOOSER_ACTION_OPEN,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                          NULL);

  if (gtk_dialog_run (GTK_DIALOG (chooser)) ==  GTK_RESPONSE_ACCEPT)
    {
      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
    }

  gtk_widget_destroy (chooser);

  if (filename)
    {
      common_infobox (GTK_WINDOW (parent), filename);
    }

  g_message ("Import configuration file ...");

}

static void
export_config_file_cb (GtkButton *button, gpointer user_data)
{

  GtkWidget *chooser, *parent;
  gchar *filename = NULL;

  parent = gtk_widget_get_toplevel (GTK_WIDGET (button));

  chooser = gtk_file_chooser_dialog_new (_("Export configuration file ..."),
                                         GTK_WINDOW (parent),
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                         NULL);

  if (gtk_dialog_run (GTK_DIALOG (chooser)) ==  GTK_RESPONSE_ACCEPT)
    {
      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
    }

  gtk_widget_destroy (chooser);

  if (filename)
    {
      common_infobox (GTK_WINDOW (parent), filename);
    }

  g_message ("Export configuration file ...");

}

static void
create_default_conf_dir (void)
{

  gchar *path, *old_pwd;

  old_pwd = g_get_current_dir ();

  path = g_strconcat (g_get_home_dir (), HOME_CONF_DIR, NULL);

  g_mkdir (path, S_IRWXU);
  g_chdir (path);
  g_free (path);

  g_mkdir ("desktop", S_IRWXU | S_IRGRP | S_IROTH); // mode 744
  g_mkdir ("icons", S_IRWXU | S_IRGRP | S_IROTH);

  g_file_copy (DATA_DIR"/gdbabar.conf.default", "gdbabar.conf");

  g_chdir (old_pwd);
  g_free (old_pwd);

}

